#include "track.h"

#include <QFileInfo>
#include <QDir>
#include <QDebug>

#include "utilities.h"

Track::Track()
{
    th = new TagHandler();
}

QStringList getCover(QString filename, QString album)
{
    QStringList sl;
    QFileInfo fi(filename);
    QDir d(fi.path());

    QFileInfoList fil = d.entryInfoList(QDir::Files);
    //qDebug()<<fi.path()<<fil.size()<<album;

    for(int i = 0; i < fil.size(); i++)
    {
    //qDebug()<<fil.at(i).completeBaseName()<<fil.at(i).suffix()<<fil.size();
    if(Utilities::isImageFileSupported(fil.at(i).suffix()))
    {
        QString s = fil.at(i).completeBaseName();

        if(s == album || s.contains("front",Qt::CaseInsensitive) == "front"
            || s.contains("front",Qt::CaseInsensitive) == "folder"
            || s.contains("front",Qt::CaseInsensitive) == "cover")
        {
        //qDebug()<<fil.at(i).absoluteFilePath();
        sl.append(fil.at(i).absoluteFilePath());
        }
    }
    }
    return sl;
}

Track::Track(QString s)
{
    th = new TagHandler();

    if(th->loadPath(s))
    {
        qDebug()<<"loadPathTrue";
        //this->source = Phonon::MediaSource(s);
        //qDebug() << "loaded";
        this->path = th->path;
        this->title = th->getTitle();
        //qDebug() << title;
        this->artist = th->getArtist();
        //qDebug() << artist;
        this->album = th->getAlbum();
        this->albumArtist = th->getAlbumArtist();
        //qDebug() << albumArtist;
        this->year = th->getYear();
        this->track = th->getTrack();
        this->label = th->getLabel();
        this->composer = th->getComposer();
        this->disc = th->getDisc();
        this->genre = th->getGenre();
        this->comment = th->getComment();
        this->mbid = "";
        this->coverPaths = getCover(s,this->album);
        this->bpm = th->getBpm().toInt();

        this->length = th->getLength();
        //qDebug() << length;
        this->bitrate = th->getBitrate();
        this->channel = th->getChannel();
        this->freq = th->getFreq();
        this->lyrics = th->getLyrics();
    }
    else
    {
        //qDebug()<<"loadPathFalse";
        //this->source = Phonon::MediaSource(s);
        this->path = s;
        this->title = s;
        this->album = "";
        this->artist = "";
        this->year = 0;
        this->track = 0;
        this->albumArtist = "";
        this->label = "";
        this->composer = "";
        this->disc = "";
        this->genre = "";
        this->comment = "";
        this->mbid = "";
        //	QFileInfo f(s);
        //	this->coverPaths = getCover(s,f.completeBaseName());
        this->length = 0;
    }

    QFileInfo fi(s);

    lastMod = fi.lastModified();
    filesize = fi.size();
    filetype = fi.suffix();
    this->plays = 0;
    this->rating = 0;

    if(th->isPathTemp)
    {
    th->isPathTemp = false;
    th->file.remove(th->tmpPath);
    }
}

QString Track::getPath()
{
    return this->path;
}

//Tags
QString Track::getTitle()
{
    return this->title;
}

QString Track::getArtist()
{
    return this->artist;
}

QString Track::getAlbum()
{
    return this->album;
}

QString Track::getAlbumArtist()
{
    return this->albumArtist;
}

QString Track::getGenre()
{
    return this->genre;
}

QString Track::getComposer()
{
    return this->composer;
}

QString Track::getLabel()
{
    return this->label;
}

int Track::getYear()
{
    return this->year;
}

int Track::getTrack()
{
    return this->track;
}

QString Track::getComment()
{
    return this->comment;
}

QString Track::getDisc()
{
    return this->disc;
}

QString Track::getLyrics()
{
    return this->lyrics;
}

QString Track::getMbid()
{
    return this->mbid;
}

QString Track::getFingerprint()
{
    return this->fingerprint;
}

QStringList Track::getCoverPaths()
{
    return this->coverPaths;
}

//audio properties
int Track::getLength()
{
    return this->length;
}

int Track::getBitrate()
{
    return this->bitrate;
}

int Track::getChannel()
{
    return this->channel;
}

int Track::getFreq()
{
    return this->freq;
}

int Track::getBpm()
{
    return this->bpm;
}

//file properties
QDateTime Track::getLastMod()
{
    return this->lastMod;
}

QDateTime Track::getLastPlay()
{
    return this->lastPlay;
}

qint64 Track::getFileSize()
{
    return this->filesize;
}

QString Track::getFileType()
{
    return this->filetype;
}

//media library properties
double Track::getRating()
{
    return this->rating;
}

double Track::getPlays()
{
    return this->plays;
}


//void Track::setSaveSource(Phonon::MediaSource source);

void Track::setSaveArtist(QString artist)
{

}

void Track::setSaveTitle(QString title)
{

}

void Track::setSaveAlbum(QString album)
{

}

void Track::setSaveAlbumArtist(QString albumArtist)
{

}

void Track::setSaveGenre(QString genre)
{

}

void Track::setSaveComposer(QString composer)
{

}

void Track::setSaveLabel(QString label)
{

}

void Track::setSaveComment(QString comment)
{

}

void Track::setSaveDisc(QString disc)
{

}

void Track::setSavePuid(QString puid)
{

}

void Track::setSaveLyrics(QString lyrics)
{

}

void Track::setSaveFingerprint(QString fingerprint)
{

}

void Track::setSaveTrack(int track)
{

}

void Track::setSaveYear(int year)
{

}

void Track::setSaveRating(double rating)
{

}

void Track::setSavePlays(double plays)
{
    //
}
